#pragma once
#include "buddy.h"

struct buddy_hash 
{
	enum
	{	// parameters for hash table
		bucket_size = 4,	// 0 < bucket_size
		min_buckets = 8	// min_buckets = 2 ^^ N, 0 < N
	};

	size_t operator()( const buddy & x ) const
	{
		return static_cast<size_t>( x.id() );
	}

	bool operator() ( const buddy & x, const buddy & y ) const
	{
		return x.id() < y.id();
	}
};

typedef hash_set<buddy, buddy_hash> buddy_set;

class buddy_manager
{
private:
	typedef set<__int64>		tag_set;
	typedef name_searcher<__int64>	searcher;
	typedef hash_map<__int64, buddy>	buddy_map;

public:
	buddy_manager(void);
	~buddy_manager(void);

public:
	void add_buddy( const buddy & b );
	void remove_buddy( const buddy & b );
	void search_buddy( const wstring & key, buddy_set & buddies ) const;

private:
	void add_name_tag( const wstring & name, __int64 tag );

	void add_pinyin_tag( const set<wstring> &pinyin, const wstring & name, __int64 tag );
	void remove_name_tag( const wstring & name, const __int64 tag );

	void remove_pinyin_tag( set<wstring> &pinyin, const __int64 tag );
private:
	buddy_map	buddies_;
	searcher	searcher_;
};
